<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #drawing {
      border: 10px solid #ccc;
    }
  </style>
</head>
<body>
  <canvas id="drawing" width="200" height="200"></canvas>

  <script>
    let context = drawing.getContext("2d");// 设置阴影
    
    // 设置 gradient 对象
    let gradient = context.createLinearGradient(30, 30, 70, 70);
    gradient.addColorStop(0, "white");
    gradient.addColorStop(1, "black");

    // 将 gradient 对象赋值给 fillStyle 创造渐变矩形
    // 绘制红色矩形
    context.fillStyle = "#ff0000";
    context.fillRect(10, 10, 50, 50);
    // 绘制渐变矩形
    context.fillStyle = gradient;
    context.fillRect(30, 30, 50, 50);
  </script>
</body>
</html>